package com.swc.filter;

import com.swc.constant.HeaderConstant;
import com.swc.property.SecretKeyProperty;
import com.swc.wrapper.DecryptReqParamWrapper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 解密请求的参数
 *
 * @author huyupeng
 * @date 2020-09-18
 */
@Component
public class DecryptReqParamFilter extends OrderedOncePerRequestFilter {

    private Logger logger = LoggerFactory.getLogger(DecryptReqParamFilter.class);

    @Autowired
    private SecretKeyProperty rsaKeyProperty;

    public DecryptReqParamFilter() {
        super(1);
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String encryptMode = request.getHeader(HeaderConstant.HEADER_ENCRYPT_MODE);
        HttpServletRequestWrapper requestWrapper = null;
        String contentType = request.getContentType();
        boolean contentTypeIsEmpty = StringUtils.isEmpty(contentType);
        //交给下一个过滤器DecryptJsonReqParamFilter处理
        if (!contentTypeIsEmpty && contentType.contains(MediaType.APPLICATION_JSON_VALUE) && !"get".equalsIgnoreCase(request.getMethod())) {
            filterChain.doFilter(request, response);
            return;
        }
        if (StringUtils.isNotEmpty(encryptMode) && !HeaderConstant.HEADER_ENCRYPT_MODE_NONE.equals(encryptMode)) {
            String secretKey = request.getHeader(HeaderConstant.HEADER_SECRET_KEY);
            if (StringUtils.isEmpty(secretKey)) {
                throw new RuntimeException("请求头中没有密钥");
            }
            if (isUseDecryptReqParamWrapper(contentTypeIsEmpty, contentType, request)) {
                logger.info(request.getQueryString());
                requestWrapper = new DecryptReqParamWrapper(request, rsaKeyProperty);
            }
        }

        if (requestWrapper != null) {
            filterChain.doFilter(requestWrapper, response);
        } else {
            filterChain.doFilter(request, response);
        }
    }

    /**
     * 是否使用解密请求参数的包装类包装request
     *
     * @param contentType
     * @param request
     * @return
     */
    private boolean isUseDecryptReqParamWrapper(boolean contentTypeIsEmpty, String contentType, final HttpServletRequest request) {
        return (HeaderConstant.HEADER_URL_PARAM_DECRYPT_YES.equals(request.getHeader(HeaderConstant.HEADER_URL_PARAM_DECRYPT)))
                || (!contentTypeIsEmpty && (contentType.contains(MediaType.APPLICATION_FORM_URLENCODED_VALUE) || contentType.contains(MediaType.MULTIPART_FORM_DATA_VALUE)));
    }


}
